package uk.co.q3c.deplan.client.domain.util;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Id;

import uk.co.q3c.deplan.client.util.Q3DateUtil;
import uk.co.q3c.deplan.client.util.Q3StringUtil;
import uk.co.q3c.deplan.client.util.apache.StringUtils;

import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Unindexed;

@Unindexed
@Cached(expirationSeconds = 600)
public abstract class DateProfileEntryBase implements Serializable, DateProfileEntry {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String dateKey;
	@Id
	private Long keyId;

	/**
	 * 
	 */
	public DateProfileEntryBase() {
		super();
	}

	/**
	 * @param dao
	 */
	public DateProfileEntryBase(String dateKey) {
		super();
		this.dateKey = dateKey;
	}

	@Override
	public String getDateKey() {
		return dateKey;
	}

	public void setDateKey(String dateKey) {
		this.dateKey = dateKey;
	}

	@Override
	public void setDate(Date date) {
		dateKey = Q3DateUtil.dateToKey(date);
	}

	@Override
	public int compareTo(Object other) {
		if (!(other instanceof DateProfileEntry)) {
			throw new RuntimeException("not comparable");
		}
		DateProfileEntry dpe = (DateProfileEntry) other;
		return Q3StringUtil.compare(dateKey, dpe.getDateKey());
	}

	@Override
	public boolean equals(Object other) {
		if (other == null) {
			return false;
		}
		if (!(other instanceof DateProfileEntry)) {
			return false;
		}

		DateProfileEntry dpe = (DateProfileEntry) other;

		return StringUtils.equals(dateKey, dpe.getDateKey());
	}

	@Override
	public String toString() {
		return dateKey;
	}

	/**
	 * Does nothing. override as necessary
	 */
	@Override
	public void clear() {

	}

	public Long getKeyId() {
		return keyId;
	}

}
